package janelas;

import Classes.*;
import javax.swing.Icon;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniela
 */
public class AlterarDados extends javax.swing.JFrame {

    private Icon foto;
    
    private ContaDoUsuario janelaAnterior;
    private Sistema sistema;
    
    public AlterarDados(ContaDoUsuario janelaAnterior) {
        initComponents();
        this.janelaAnterior = janelaAnterior;
        setResizable(false);
        setLocationRelativeTo(null);
        
        sistema = janelaAnterior.getSistema();
        foto = lFoto3.getIcon();
        
        dadosUsuario();
    }
    
    public void recebeFoto(Icon foto) {
        this.foto = foto;
        lFoto3.setIcon(foto);
    }
     
    public Sistema getSistema() { 
        return janelaAnterior.getSistema();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollBar1 = new javax.swing.JScrollBar();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        bNome = new javax.swing.JTextField();
        comboxSexo = new javax.swing.JComboBox();
        bSala = new javax.swing.JTextField();
        bBloco = new javax.swing.JTextField();
        bTelefone = new javax.swing.JFormattedTextField();
        bSenha = new javax.swing.JPasswordField();
        bNovaSenha1 = new javax.swing.JPasswordField();
        bNovaSenha2 = new javax.swing.JPasswordField();
        lFoto3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        bSalvarAlteracoes = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        bPesquisarFoto = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alterar Dados");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));
        jPanel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setFont(new java.awt.Font("Franklin Gothic Medium Cond", 1, 18)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/janelas/IconAlterarDados1.jpg"))); // NOI18N
        jLabel1.setText("Alterar Dados");

        jLabel2.setText("Nome completo");

        jLabel3.setText("Sexo");

        jLabel4.setText("Sala");

        jLabel5.setText("Bloco");

        jLabel6.setText("Telefone");

        jLabel7.setText("Senha Atual");

        jLabel8.setText("Nova Senha");

        jLabel9.setText("Redigitar nova senha");

        bNome.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                bNomeComponentShown(evt);
            }
        });

        comboxSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma opção", "Feminino", "Masculino" }));

        bSala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSalaActionPerformed(evt);
            }
        });

        try {
            bTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(###)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        lFoto3.setText("                     Foto");

        jButton2.setText("Alterar localização no mapa?");

        bSalvarAlteracoes.setText("Salvar Alterações");
        bSalvarAlteracoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSalvarAlteracoesActionPerformed(evt);
            }
        });

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(153, 153, 255));
        jLabel11.setText("Mínimo 6 caracteres");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(153, 153, 255));
        jLabel12.setText("(083)3333-3333");

        bPesquisarFoto.setText("Pesquisar...");
        bPesquisarFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPesquisarFotoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(179, 179, 179)
                        .addComponent(jLabel13)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(bNome)
                                                .addComponent(bNovaSenha2)
                                                .addComponent(comboxSexo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(bSala)
                                                .addComponent(bBloco)
                                                .addComponent(bTelefone)
                                                .addComponent(bSenha)
                                                .addComponent(bNovaSenha1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(jLabel12))
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addGap(58, 58, 58)
                                                .addComponent(bPesquisarFoto))
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addGap(18, 18, 18)
                                                .addComponent(lFoto3, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addComponent(jButton2)
                                    .addComponent(jLabel11))
                                .addGap(16, 16, 16))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(bSalvarAlteracoes)
                                .addGap(18, 18, 18)
                                .addComponent(bCancelar)))))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(jLabel1))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(bNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(comboxSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bSala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bBloco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lFoto3, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(bTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(bPesquisarFoto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(bSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(bNovaSenha1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(bNovaSenha2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(49, 49, 49)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bSalvarAlteracoes)
                    .addComponent(bCancelar))
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bSalvarAlteracoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSalvarAlteracoesActionPerformed
        alterarDados();        
    }//GEN-LAST:event_bSalvarAlteracoesActionPerformed

    private void bPesquisarFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPesquisarFotoActionPerformed
        new JanelaRecebeFoto(this).setVisible(true);
    }//GEN-LAST:event_bPesquisarFotoActionPerformed
    private void bSalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSalaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bSalaActionPerformed

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
        janelaAnterior.setEnabled(true);
        janelaAnterior.setVisible(true);
    }//GEN-LAST:event_bCancelarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        janelaAnterior.setVisible(true);
        janelaAnterior.setEnabled(true);
    }//GEN-LAST:event_formWindowClosed

    private void bNomeComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_bNomeComponentShown
      
    }//GEN-LAST:event_bNomeComponentShown
    
    boolean status = true;
    public void alterarDados(){
        String senhaAtual = String.valueOf(bSenha.getPassword());
        String novaSenha1 = String.valueOf(bNovaSenha1.getPassword());
        String novaSenha2 = String.valueOf(bNovaSenha2.getPassword());
  
            if(!bNome.getText().equals(""))
               sistema.getUsuarioAtivo().getPessoa().setNome(bNome.getText());
            if(comboxSexo.getSelectedIndex() == 1 || comboxSexo.getSelectedIndex() == 2)
                sistema.getUsuarioAtivo().getPessoa().setGenero(genero(comboxSexo.getSelectedIndex()));
            if(!bSala.getText().equals(""))
                sistema.getUsuarioAtivo().getLocal().setSala(bSala.getText());
            if(!bBloco.getText().equals(""))
                sistema.getUsuarioAtivo().getLocal().setBloco(bBloco.getText());
            if(!bTelefone.getText().equals(""))
                sistema.getUsuarioAtivo().getPessoa().setTelefone(bTelefone.getText());
            if(lFoto3.getIcon() != null)
                sistema.getUsuarioAtivo().getPessoa().setFoto(lFoto3.getIcon());
            
            if(!senhaAtual.equals("")){
                if(!senhaAtual.equals(sistema.getUsuarioAtivo().getSenha())){
                    JOptionPane.showMessageDialog(null, "Senha atual incorreta!");
                    bSenha.setText("");
                    status = false;
                }else{
                    String mensagemDeErro = "";
                    if(!novaSenha1.equals("")){
                        if(novaSenha1.replaceAll(" ", "").length() < 6){
                            mensagemDeErro += "A senha deve conter no mínimo 6 caracteres!";
                            bNovaSenha1.setText("");
                            bNovaSenha2.setText("");
                        }else if(!novaSenha1.equals(novaSenha2)){
                            mensagemDeErro += "Senhas não correspondem!";
                            bNovaSenha1.setText("");
                            bNovaSenha2.setText("");
                        }
                        if(!mensagemDeErro.equals("")){
                            JOptionPane.showMessageDialog(null, mensagemDeErro);
                            status = false;
                            
                        }else{
                            sistema.getUsuarioAtivo().setSenha(String.valueOf(bNovaSenha1.getPassword()));
                        }
                    }
                }  
            }
            
            String mensagemDeErro2 = "";
            
            if(bNome.getText().equals("")){
                mensagemDeErro2 += "Nome não pode ser vazio!\n";
            }
            if(comboxSexo.getSelectedIndex() == 0){
                mensagemDeErro2 += "O sexo deve ser selecionado!\n";
            }
            if(bSala.getText().equals("")){
                mensagemDeErro2 += "Sala não pode ser vazio!\n";
            }
            if(bBloco.getText().equals("")){
                mensagemDeErro2 += "Bloco não pode ser vazio!\n";
            } 
            if(bTelefone.getText().replaceAll(" ", "").equals("()-")){
               mensagemDeErro2 += "Telefone não pode ser vazio!";
            }
            if(!mensagemDeErro2.equals("")){
                JOptionPane.showMessageDialog(null, mensagemDeErro2);
            }
                           
            if(status){
                JOptionPane.showMessageDialog(null, "Dados alterados com sucesso."); 
                janelaAnterior.setVisible(true);
                janelaAnterior.atualizar();
                janelaAnterior.setEnabled(true);
                this.dispose();
            }
    }
    
    public void dadosUsuario(){
        bNome.setText(sistema.getUsuarioAtivo().getPessoa().getNome());
        bSala.setText(sistema.getUsuarioAtivo().getLocal().getSala());
        bBloco.setText(sistema.getUsuarioAtivo().getLocal().getBloco());
        bTelefone.setText(sistema.getUsuarioAtivo().getPessoa().getTelefone());
        switch (sistema.getUsuarioAtivo().getPessoa().getGenero()) {
            case "Feminino":
                comboxSexo.setSelectedIndex(1);
                break;
            case "Masculino":
                comboxSexo.setSelectedIndex(2);
                break;
        }
        lFoto3.setIcon(sistema.getUsuarioAtivo().getPessoa().getFoto());
    }
    
    public char genero(int sexo){
        if(sexo == 1){
            return 'f';
        }else if(sexo == 2){
            return 'm';
        }
        return ' ';
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField bBloco;
    private javax.swing.JButton bCancelar;
    private javax.swing.JTextField bNome;
    private javax.swing.JPasswordField bNovaSenha1;
    private javax.swing.JPasswordField bNovaSenha2;
    private javax.swing.JButton bPesquisarFoto;
    private javax.swing.JTextField bSala;
    private javax.swing.JButton bSalvarAlteracoes;
    private javax.swing.JPasswordField bSenha;
    private javax.swing.JFormattedTextField bTelefone;
    private javax.swing.JComboBox comboxSexo;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JLabel lFoto3;
    // End of variables declaration//GEN-END:variables
}
